package com.nvshen.zhangmeiying.life;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.nvshen.zhangmeiying.BaseActivity;
import com.nvshen.zhangmeiying.Constants;
import com.nvshen.zhangmeiying.R;
import com.nvshen.zhangmeiying.model.Article;
import com.nvshen.zhangmeiying.utils.NetUtils;
import com.nvshen.zhangmeiying.utils.PerfectAsyncHandler;
import com.nvshen.zhangmeiying.utils.UrlUtils;
import com.nvshen.zhangmeiying.utils.xlist.XListView;
import org.apache.http.Header;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lixinbo on 14-10-8.
 */
public class LifeActivity extends BaseActivity implements XListView.IXListViewListener
{
    private int curPage = 0;

    XListView listView;
    HotListAdapter adapter;
    ArrayList<Article> articleList = new ArrayList<Article>();
    View nullView;

//    User curUid;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_life);

//        curUid = (User) getIntent().getSerializableExtra("extra_user");
//
//        if (curUid == null)
//        {
//            Toast.makeText(this, "获取用户失败", Toast.LENGTH_SHORT).show();
//            getParent().finish();
//        }

        initViews();
        getHotListRefresh();

    }

    private void initViews()
    {
        nullView = findViewById(R.id.feed_null_view);

        findViewById(R.id.head_left).setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                getParent().finish();
            }
        });

        listView = (XListView) findViewById(R.id.listView);
        listView.setPullRefreshEnable(true);
        listView.setPullLoadEnable(true);
        listView.setXListViewListener(this);
        adapter = new HotListAdapter(this);
        adapter.setList(articleList);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id)
            {
//                ArticleDetailActivity.getInstance(ProfileLifeActivity.this, articleList.get(position - 1));
            }
        });
    }

    @Override
    public void onRefresh()
    {
        getHotListRefresh();
    }

    @Override
    public void onLoadMore()
    {
        getHotListMore(curPage);
    }

    private void getHotListRefresh()
    {
        listView.setPullLoadEnable(true);
        NetUtils.get(UrlUtils.getMyArticle(0, Constants.NVSHEN_UID), new PerfectAsyncHandler()
        {
            @Override
            public void onGetSuccess(Header[] header, String result)
            {
                listView.stopRefresh();

                LifeActivity.this.curPage = 1;
                articleList = new Gson().fromJson(result, new TypeToken<List<Article>>()
                {
                }.getType());
                refreshViews();
                adapter.setList(articleList);

                if (articleList.size() % Constants.COUNT_IN_PAGE != 0)
                {
                    listView.setPullLoadEnable(false);
                }
            }

            @Override
            public void onGetFailure(String errorCause)
            {
            }
        });
    }

    private void refreshViews()
    {
        if (articleList.size() > 0)
        {
            nullView.setVisibility(View.INVISIBLE);
            listView.setVisibility(View.VISIBLE);
        }
        else
        {
            nullView.setVisibility(View.VISIBLE);
            listView.setVisibility(View.INVISIBLE);
        }
    }

    private void getHotListMore(int pageIndex)
    {
        NetUtils.get(UrlUtils.getMyArticle(pageIndex, Constants.NVSHEN_UID), new PerfectAsyncHandler()
        {
            @Override
            public void onGetSuccess(Header[] header, String result)
            {
                listView.stopLoadMore();

                LifeActivity.this.curPage += 1;
                articleList.addAll((java.util.Collection<? extends Article>) new Gson().fromJson(result, new TypeToken<List<Article>>()
                {
                }.getType()));
                refreshViews();
                adapter.notifyDataSetChanged();

                if (articleList.size() % Constants.COUNT_IN_PAGE != 0)
                {
                    listView.setPullLoadEnable(false);
                }
            }

            @Override
            public void onGetFailure(String errorCause)
            {

            }
        });
    }
}